//
//  APIMycityHistory.m
//  Bracelet
//
//  Created by jing zhao on 2/26/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "APIMycityHistory.h"
#import "APIConnection.h"

#import <AFNetworking/AFNetworking.h>

#import "FileReadAndWrite.h"

#define FILENAMECACHE_APIMYCITYHISTORY @"fileName_apiMycityHistory"

@implementation APIMycityOneHistory

-(instancetype)initFromDictionary:(NSDictionary*)dicHistoryOne
{
    self = [super init];
    if (self) {
        self.strCitySUmMeters = [dicHistoryOne objectForKey:APIKEYCITY_SUM_METRES];
        self.strCityProportion = [dicHistoryOne objectForKey:APIKEYPROPORTION];
        self.strRunDate = [dicHistoryOne objectForKey:APIKEYRUN_DATE];
    }
    return self;
    
}
+(instancetype)apiOneHistoryFormDictionary:(NSDictionary*)dicHistoryOne
{
    return [[self alloc]initFromDictionary:dicHistoryOne];
}

@end
@implementation APIMycityHistory

-(instancetype)initFromDictionary:(NSDictionary*)dic
{
    self = [super init];
    if (self ) {
        self.strHtml = [dic objectForKey:APIKEYHTML];
//        self.strMaxmeter = [dic objectForKey:APIKEYMAXMETRE];
//        NSArray *arrayData = [dic objectForKey:APIKEYWEEK_DATACITY];
//        _arrayHistroyDataList = [[NSMutableArray alloc]init];
//        for (NSDictionary *dicTemp in arrayData) {
//            APIMycityOneHistory *oneHisitory = [APIMycityOneHistory apiOneHistoryFormDictionary:dicTemp];
//            [self.arrayHistroyDataList addObject:oneHisitory];
//        }
    }
    return self;
}

+(instancetype)apiTodayWithData:(NSDictionary*)dic
{
    return [[APIMycityHistory alloc] initFromDictionary:dic];

}
+(void)getMycityHistrorySuccess:(void (^)(APIMycityHistory *mycityHistory))successBlock
                        failure:(void (^)( NSError *error)) failureBlock
{
    NSString *strCityId = [[[NSUserDefaults standardUserDefaults]objectForKey:APIKEYUSERINFO]objectForKey:APIKEYCITYID];
    NSDictionary *data = @{@"flag":@"mycity",@"cityId":strCityId};
    
    [[APIConnection shareInstance]post:@"mycity/getHistoryData_city" data:data netError:^(NSError *error) {
        failureBlock(error);
        
        //取缓存
        NSDictionary *dicCanch = [FileReadAndWrite getTheStorageDataWithFile:FILENAMECACHE_APIMYCITYHISTORY];
        if (dicCanch) {
            NSDictionary *dicSuccess = [dicCanch objectForKey:APIKEYData];
            successBlock([APIMycityHistory apiTodayWithData:dicSuccess]);
        }
    } apiFinish:^(id data) {
        
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [dictionaryTemp objectForKey:APIKEYData];
            successBlock([APIMycityHistory apiTodayWithData:dicSuccess]);
            
            //存入缓存
            [FileReadAndWrite storageDataWithFile:FILENAMECACHE_APIMYCITYHISTORY andData:dictionaryTemp];
        }
        else
        {
            failureBlock(nil);
        }
    }];


}
@end
